package com.example.ekupiandroid;

import org.json.JSONException;
import org.json.JSONObject;

import com.example.ekupiandroid.R;
import com.example.ekupiandroid.RegistracijaActivity;
import com.example.ekupiandroid.WebServiceRequest;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Login extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		
		
		initialize();
	}
	
	private void initialize() {
		// TODO Auto-generated method stub
		final EditText etUsername;
		final EditText etPassword;
		Button bLogin, bRegistracija;
		
		etUsername = (EditText)findViewById(R.id.etUsername);
		etPassword =  (EditText)findViewById(R.id.etPassword);
		bLogin =  (Button)findViewById(R.id.bLogin);
		bRegistracija =  (Button)findViewById(R.id.bRegistracija);
		
		bRegistracija.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent registracija = new Intent(getBaseContext(), RegistracijaActivity.class);
				startActivity(registracija);
			}
		});
		
		
		bLogin.setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						Thread t = null;
						try {
							t = new Thread (new Runnable() {
								@Override
								public void run() {
							
									String odgovor = null;
									final String naziv;
									try {
										WebServiceRequest wst = new WebServiceRequest(WebServiceRequest.GET_TASK);
										wst.AddHeader("username", etUsername.getText().toString());
										wst.AddHeader("password", etPassword.getText().toString());
										odgovor = wst.Execute("http://10.0.2.2:8080/eKupovina/webservices/korisnici/login");
									} catch (Exception e1) {
										// TODO Auto-generated catch block
										e1.printStackTrace();
									}
									try {
										if(odgovor != null){
											JSONObject json = new JSONObject(odgovor);
											//Toast.makeText(getApplicationContext(), "asdasf", Toast.LENGTH_LONG).show();
											//String korisnik = json.toString();
											
											Intent start = new Intent(getBaseContext(), Start.class);
											start.putExtra("korisnik", json.toString());
											
											startActivity(start);
										}
										else {
											
										}
								
									} catch (JSONException e) {
										// 	TODO Auto-generated catch block
										e.printStackTrace();
									}
								}
							});
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						t.start();
						
						
					}
				});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.login, menu);
		return true;
	}

}
